package com.junzhiit.nengji.app.web.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.util.AssertUtil;
import com.junzhiit.nengji.app.service.CouponsService;
import com.junzhiit.nengji.app.web.vo.CouponsAddVO;
import com.junzhiit.nengji.app.web.vo.CouponsEditVO;
import com.junzhiit.nengji.app.web.vo.CouponsSearchVO;
import com.junzhiit.nengji.app.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.app.web.vo.ResultVO;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/coupons")
public class CouponsController extends BaseController {

	@Autowired
	private CouponsService couponsService;

	@ApiOperation("查询优惠卷")
	@RequestMapping(method = RequestMethod.GET)
	public Map<String, Object> findAll(DataTablesParamVO dtp, CouponsSearchVO couponsVO) {
		AssertUtil.notBlank(couponsVO.getSessionId(), "sessionId is required");
		AssertUtil.notNull(couponsVO.getIsChain(), "isChain is required");
		return couponsService.findAll(dtp, couponsVO);
	}

	@ApiOperation("查询单个优惠卷")
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public ResultVO findById(@PathVariable String id) {
		AssertUtil.notBlank(id, "id is required");
		return couponsService.findById(id);
	}

	@ApiOperation("添加优惠卷")
	@RequestMapping(method = RequestMethod.POST)
	public ResultVO add(CouponsAddVO couponsAddVO) {
		AssertUtil.notNull(couponsAddVO.getType(), "type is required");
		AssertUtil.notNull(couponsAddVO.getIsOpenRandom(), "isOpenRandom is required");
		AssertUtil.notNull(couponsAddVO.getThreshold(), "threshold is required");
		AssertUtil.notNull(couponsAddVO.getValidTimeState(), "validTimeState is required");
		AssertUtil.notNull(couponsAddVO.getIsOpenRemind(), "isOpenRemind is required");

		return couponsService.add(couponsAddVO);
	}

	@ApiOperation("修改优惠卷")
	@RequestMapping(method = RequestMethod.PUT)
	public ResultVO edit(CouponsEditVO couponsEditVO) {
		AssertUtil.notBlank(couponsEditVO.getId(), "id is required");
		return couponsService.edit(couponsEditVO);
	}

	@ApiOperation("更新优惠卷状态")
	@RequestMapping(value = "/status", method = RequestMethod.PUT)
	public ResultVO updateStatus(String id, Integer status) {
		AssertUtil.notBlank(id, "id is required");
		AssertUtil.notNull(status, "status is required");
		return couponsService.updateStatus(id, status);
	}
	
	@ApiOperation("生成二维码")
	@RequestMapping(value = "/qrcode",method = RequestMethod.POST)
	public ResultVO qrcode(String id){
		AssertUtil.notBlank(id, "id is required");
		return couponsService.createQRCode(id);
	}
	
	@ApiOperation("二维码下载")
	@RequestMapping(value = "/qrcode/download" ,method = RequestMethod.POST)
	public void downloadQRCode(HttpServletRequest request,HttpServletResponse response,String qrcodeName,String couponsName){
		couponsService.downloadQRCode(request, response, qrcodeName,couponsName);
	}
}
